/*提交表单*/
function sendForm(formId, check, mode) {
	if(typeof formId == 'object') {
		var btn = formId;
		form = btn.closest('form');
		btn.addClass('disabled').data('txt', btn.text()).html("<i class='ace-icon fa fa-spinner fa-pulse'></i> 处理中...").prop('disabled', true);

	} else {
		form = $("#" + formId);
	}

	//检查
	check = check ? check : form.data('check');
	mode = mode ? mode : form.data('mode');
	if(check && !checkForm(form)) {
		btn.removeClass('disabled').text(btn.data('txt')).prop('disabled', false);
		return false;
	}
	post_url = form.prop('action');
	if(form.data('ajax')) {
		//		var vars = $("#" + formId).serialize();
		var vars = new FormData(form[0]); 
		Load();
		$.ajax({
			type: "POST",
			url: post_url,
			data: vars,
			contentType: false,
			processData: false,
			//			dataType : "json",
			success: function(res) {
				if(res.code==0){
					if(btn) {
						btn.removeClass('disabled').text(btn.data('txt')).prop('disabled', false);
					}
				}
				if(res.data && res.data.tip && !res.code) {
					var tipt = $('#' + res.data['tip']);
					Tips(res.msg, tipt);
					tipt.closest('.form-group').addClass('has-error');
					
				} else {
					rslt(res, mode);
					
				}

				Load(1);
			}
		});
	} else {
		//		$("#" + formId).attr("action", post_url);
		if(!form.prop('method')) form.prop('method', 'post');
		form.submit();
	}
}
function Tips(msg,target,err) {
	Err(msg ? msg : '操作失败，稍候再试');
}
function goTo(url, newopen) {
	if(url == -1) {
		history.back();
	} else if(url == 0) {
		return false
	} else if(url == 1) {
		window.location.reload();
	} else if(url) {
		if(newopen) {
			window.open(url);
		} else {
			window.location.href = url;
		}

	}
}

function rslt(res, mode, formId) {

	var newopen = 0;
	var mode = 1;
	//	Load(1);

	if(res.code) {
		rmsg = res.msg ? res.msg : '操作成功！';
		url = res.url ? res.url : res.data.url;
		
			if(mode) {
			Msg(rmsg);
			setTimeout(function(){
			goTo(url);
			},1000)
		} else {}
		
		

	} else {
		Err(res.msg ? res.msg : '操作失败，稍候再试');

	}
}

function checkForm(formId) {

	var check_flag = true;

	if(typeof formId == 'object') {
		form = formId;
	} else {
		form = $("#" + formId);
	}

	form.find(":input").each(function(i) {
		if($(this).data("check")) {
			var v;
			if($(this).prop('type') == 'checkbox' || $(this).prop('type') == 'radio') {
				v = $(this).prop('checked');
			} else {
				v = $(this).val();
			}
			if(!validate(v, $(this).data("check"))) {
				
				$(this).data("msg", $(this).data("msg") ? $(this).data("msg") : $(this).prop("placeholder"));
				if($(this).data("msg").indexOf("|")) {
					tmp = $(this).data("msg").split("|");
					msgtype = tmp[1];
					msg = tmp[0];
				}

				if(msgtype == 'popup') {
					LErr(tmp[0]);
				} else {
					
					
					Tips($(this).data("msg"),$(this),1);
					$(this).focus();
					$(this).closest('label').addClass('has-error');
				}
				//提示
				check_flag = false;
				return check_flag;
			} else {
				//				$(this).closest('label').replaceClass('has-error','has-success');
				$(this).parent().next().html('')
			}
		}
	})
	return check_flag;
};

/* 验证数据类型*/
function validate(data, datatype) {
	if(datatype.indexOf("|")) {
		tmp = datatype.split("|");
		datatype = tmp[0];
		data2 = tmp[1];
	}
	switch(datatype) {
		case "require":
			if(!data) {
				return false;
			} else {
				return true;
			}
			break;
		case "email":
			var reg = /^([0-9A-Za-z\-_\.]+)@([0-9a-z]+\.[a-z]{2,3}(\.[a-z]{2})?)$/g;
			return reg.test(data);
			break;
		case "mobile":
			var reg = /^1[34578]\d{9}$/g;
			return reg.test(data);
			break;
		case "number":
			var reg = /^[0-9]+\.{0,1}[0-9]{0,3}$/;
			return reg.test(data);
			break;
		case "html":
			var reg = /<...>/;
			return reg.test(data);
			break;
		case "gt":
			return data > data2
			break;
		case "notgt":
			return data <= data2
			break;
		case "lt":
			return data < data2
			break;
		case "notlt":
			return data >= data2
			break;
		case "eqt":
			data2 = $("#" + data2).val();
			return data >= data2
			break;
		case "password":
			var reg = /^.{6,16}$/;
			return reg.test(data);
			break;
		case "username":
			var reg = /^[a-z][0-9a-z]{4,}$/g;
			return reg.test(data);
			break;
		case "nickname":
			var reg = /^.{4,}$/g;
			return reg.test(data);
			break;
		case "code":
			var reg = /^[0-9]{4}$/;
			return reg.test(data);
			break;
	}
}

function Msg(msg, func) {
	layer.msg(msg, {
		icon: 1
	}, func);
}

function Err(msg) {
	layer.msg(msg, {
		icon: 2
	});
}

function Load(close) {
	if(close) {
		layer.closeAll('loading');
	} else {
		layer.load(0, {
			shade: false,
		});
	}
};
//点击弹出框
function popup(obj, url, number) {
	console.log(url)
	var active = {
		//删除单个
		delete: function(othis, number, url) {
			var type = othis.data('type'),
				text = othis.text();

			layer.open({
				type: 1,
				title: false,
				offset: type //具体配置参考：http://www.layui.com/doc/modules/layer.html#offset
					,
				id: "delete" //防止重复弹出
					,
				content: "<div class='am-vertical-align-middle' style='width:100%;text-align:center'>是否删除</div>",
				btn: ['确定', '取消'],
				btnAlign: 'c' //按钮居中
					,
				shade: 0 //不显示遮罩
					,
				yes: function() {
					console.log(othis.closest('tr').data('id'))
					var data = {
						id: othis.closest('tr').data('id')
					};
					var  deleteload= layer.load(1, {
					  shade: [0.1,'#fff'] //0.1透明度的白色背景
					});
					$.ajax({
						method: "POST",
						url: url,
						data: data,
						success: function(res) {
							if(res.code == 1) {
								layer.close(deleteload);
								othis.closest('tr').remove();
								layer.closeAll();
							}else if(res.code == 0){
								layer.close(deleteload);
								Err(res.msg ? res.msg : '操作失败，稍候再试');
							}

						}
					});

				},
				btn2: function() {
					layer.closeAll();
				}
			});
		},
		//删除多个
		deleteMultiple: function(othis, number, url) {
			var type = othis.data('type'),
				text = othis.text();

			layer.open({
				type: 1,
				title: false,
				offset: type //具体配置参考：http://www.layui.com/doc/modules/layer.html#offset
					,
				id: "delete" //防止重复弹出
					,
				content: "<div class='am-vertical-align-middle' style='width:100%;text-align:center'>是否删除</div>",
				btn: ['确定', '取消'],
				btnAlign: 'c' //按钮居中
					,
				shade: 0 //不显示遮罩
					,
				yes: function() {
					var deletearr=[]
					console.log($(othis).parent().next('table'))
					for(var deleteidx=0;deleteidx<$(othis).parent().next('table').find('.check-list').length;deleteidx++){
						if($(othis).parent().next('table').find('.check-list')[deleteidx].checked){
							deletearr.push($(othis).parent().next('table').children('tbody').find('tr').eq(deleteidx).data('id'))
						}
					}
					
					var data = {
						id: deletearr
					};
					console.log(data)
					var  deleteload= layer.load(1, {
					  shade: [0.1,'#fff'] //0.1透明度的白色背景
					});
					$.ajax({
						method: "POST",
						url: url,
						data: data,
						success: function(res) {
							if(res.code == 1) {
								layer.close(deleteload);
								layer.closeAll();
								rslt(res)
								window.history.go(0)
							}else if(res.code == 0){
								layer.close(deleteload);
								Err(res.msg ? res.msg : '操作失败，稍候再试');
							}

						}
					});

				},
				btn2: function() {
					layer.closeAll();
				}
			});
		},
		//取消
		cancel: function(othis, number, url) {
			var type = othis.data('type'),
				text = othis.text();

			layer.open({
				type: 1,
				title: false,
				offset: type //具体配置参考：http://www.layui.com/doc/modules/layer.html#offset
					,
				id: "delete" //防止重复弹出
					,
				content: "<div class='am-vertical-align-middle' style='width:100%;text-align:center'>是否取消该订单</div>",
				btn: ['确定', '取消'],
				btnAlign: 'c' //按钮居中
					,
				shade: 0 //不显示遮罩
					,
				yes: function() {
					console.log(othis.closest('tr').data('id'))
					var data = {
						id: othis.closest('tr').data('id'),
						state:12
					};
					var  deleteload= layer.load(1, {
					  shade: [0.1,'#fff'] //0.1透明度的白色背景
					});
					$.ajax({
						method: "POST",
						url: url,
						data: data,
						success: function(res) {
							console.log(res)
							if(res.code == 1) {
								layer.close(deleteload);
								layer.closeAll();
								window.history.go(0)
							}else if(res.code == 0){
								layer.close(deleteload);
								Err(res.msg ? res.msg : '操作失败，稍候再试');
							}

						}
					});

				},
				btn2: function() {
					layer.closeAll();
				}
			});
		},
		//同意
		consent: function(othis, number, url) {
			var type = othis.data('type'),
				text = othis.text();

			layer.open({
				type: 1,
				title: false,
				offset: type //具体配置参考：http://www.layui.com/doc/modules/layer.html#offset
					,
				id: "consent" //防止重复弹出
					,
				content: "<div class='am-vertical-align-middle' style='width:100%;text-align:center'>是否同意该订单</div>",
				btn: ['确定', '取消'],
				btnAlign: 'c' //按钮居中
					,
				shade: 0 //不显示遮罩
					,
				yes: function() {
					console.log(othis.closest('tr').data('id'))
					var data = {
						id: othis.closest('tr').data('id'),
						state:4
					};
					var  deleteload= layer.load(1, {
					  shade: [0.1,'#fff'] //0.1透明度的白色背景
					});
					$.ajax({
						method: "POST",
						url: url,
						data: data,
						success: function(res) {
							console.log(res)
							if(res.code == 1) {
								layer.close(deleteload);
								layer.closeAll();
								window.history.go(0)
							}else if(res.code == 0){
								layer.close(deleteload);
								Err(res.msg ? res.msg : '操作失败，稍候再试');
							}

						}
					});

				},
				btn2: function() {
					layer.closeAll();
				}
			});
		},
		
		//拒绝
		repulse: function(othis, number, url) {
			var type = othis.data('type'),
				text = othis.text();

			layer.open({
				type: 1,
				title: false,
				offset: type //具体配置参考：http://www.layui.com/doc/modules/layer.html#offset
					,
				id: "repulse" //防止重复弹出
					,
				content: "<div class='am-vertical-align-middle' style='width:100%;text-align:center'>是否拒绝该订单</div>",
				btn: ['确定', '取消'],
				btnAlign: 'c' //按钮居中
					,
				shade: 0 //不显示遮罩
					,
				yes: function() {
					console.log(othis.closest('tr').data('id'))
					var data = {
						id: othis.closest('tr').data('id'),
						state:3
					};
					var  deleteload= layer.load(1, {
					  shade: [0.1,'#fff'] //0.1透明度的白色背景
					});
					$.ajax({
						method: "POST",
						url: url,
						data: data,
						success: function(res) {
							console.log(res)
							if(res.code == 1) {
								layer.close(deleteload);
								layer.closeAll();
								window.history.go(0)
							}else if(res.code == 0){
								layer.close(deleteload);
								Err(res.msg ? res.msg : '操作失败，稍候再试');
							}

						}
					});

				},
				btn2: function() {
					layer.closeAll();
				}
			});
		},
		
		//启用单个
		startusing: function(othis, number, url) {
			var type = othis.data('type'),
				text = othis.text();

			layer.open({
				type: 1,
				title: false,
				offset: type 
					,
				id: "delect" 
					,
				content: "<div class='am-vertical-align-middle' style='width:100%;text-align:center'>是否启用</div>",
				btn: ['确定', '取消'],
				btnAlign: 'c' //按钮居中
					,
				shade: 0 //不显示遮罩
					,
				yes: function() {
					var data = {
						id: othis.closest('tr').data('id'),
						state: 1
					};
					var  startusingload= layer.load(1, {
					  shade: [0.1,'#fff'] //0.1透明度的白色背景
					});
					$.ajax({
						method: "POST",
						url: url,
						data: data,
						success: function(res) {
							if(res.code == 1) {
								layer.closeAll();
									window.history.go(0) 
							}else if(res.code == 0){
								layer.close(startusingload);
								Err(res.msg ? res.msg : '操作失败，稍候再试');
							}

						}
					});

				},
				btn2: function() {
					layer.closeAll();
				}
			});
		},
		//禁用单个
		forbidden: function(othis, number, url) {
			var type = othis.data('type'),
				text = othis.text();

			layer.open({
				type: 1,
				title: false,
				offset: type //具体配置参考：http://www.layui.com/doc/modules/layer.html#offset
					,
				id: "delect" //防止重复弹出
					,
				content: "<div class='am-vertical-align-middle' style='width:100%;text-align:center'>是否禁用</div>",
				btn: ['确定', '取消'],
				btnAlign: 'c' //按钮居中
					,
				shade: 0 //不显示遮罩
					,
				yes: function() {
					var  forbiddenload= layer.load(1, {
					  shade: [0.1,'#fff'] //0.1透明度的白色背景
					});
					var data = {
						id: othis.closest('tr').data('id'),
						state: 9
					};
					$.ajax({
						method: "POST",
						url: url,
						data: data,
						success: function(res) {
							if(res.code == 1) {
								layer.closeAll();
								window.history.go(0) 
							}else if(res.code == 0){
								layer.close(forbiddenload);
								Err(res.msg ? res.msg : '操作失败，稍候再试');
							}

						}
					});
				},
				btn2: function() {
					layer.closeAll();
				}
			});
		},
		showmap: function(othis) {
			var type = othis.data('type'),
				text = othis.text();

			layer.open({
				type: 1,
				area: ['800px', '800px'],
				title: false,
				offset: type //具体配置参考：http://www.layui.com/doc/modules/layer.html#offset
					,
				id: 'layerDemo' + type //防止重复弹出
					,
				content: '<div id="showlocation" style="width:99%;height:99%;margin:1%"></div>',
				btn: ['确认'],
				success: function(layero) {
					var btn = layero.find('.layui-layer-btn');
					//					layer.closeAll();
				},
				btn2: function() {
					layer.closeAll();
				},
				btnAlign: 'c' //按钮居中
					,
				shade: 0 //不显示遮罩
					,
				yes: function() {
					layer.closeAll();
				}
			});
		}

	}
	
	var othis = $(obj),
		method = othis.data('method');
	active[method] ? active[method].call(obj, othis, number, url) : '';
	
};

//全选
function checkall(obj) {
	var idname = $(obj).closest('table').attr('id')
	if($(obj).data('checkall')) {
		for(var i = 0; i < $('#' + idname + ' tbody input[type="checkbox"]').length; i++) {
			$('#' + idname + ' tbody input[type="checkbox"]')[i].checked = obj.checked
		}
	} else {
		var ispitchon = true
		for(var i = 0; i < $('#' + idname + ' tbody input[type="checkbox"]').length; i++) {
			if($('#' + idname + ' tbody input[type="checkbox"]')[i].checked) {

			} else {
				ispitchon = false
			}
		}
		if(ispitchon) {
			$('table thead input')[0].checked = true
		} else {
			$('table thead input')[0].checked = false
		}
	}
};
//选择车辆
function choosecar(url){
	console.log(url)
	layer.open({
	  type: 2,
	  title: '请选择',
	  shadeClose: true,
	  shade: 0.8,
	  area: ['80%', '70%'],
	  content: url //iframe的url
	}); 
}
//公司部门编辑
function department(url,msg){
	layer.open({
	  type: 2,
	  title: msg?msg:'请选择',
	  shadeClose: true,
	  shade: 0.8,
	  area: ['50%', '70%'],
	  content: url //iframe的url
	}); 
}

//上传图片前预览
function preview(file) {
		var prevDiv = $(file).closest('.showimg').children('div')[0];
		console.log(prevDiv)
		if(file.files && file.files[0]) {
			var reader = new FileReader();
			reader.onload = function(evt) {
				prevDiv.innerHTML = '<img src="' + evt.target.result + '" alt="车辆正面"/>';
			}
			reader.readAsDataURL(file.files[0]);
		} else {
			prevDiv.innerHTML = '<div class="img" style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale,src=\'' + file.value + '\'"></div>';
		}
	}

//时间判断
function datejudge(obj1,obj2){
  if(obj1){
  	if(obj2){
  		var dat1=obj1.val()
  		var dat2=obj2.val()
  		dat1=new Date(dat1)
  		dat2=new Date(dat2)
  		if(dat1>dat2){
  			Err('开始时间要在结束时间之前')
  			return false
  		}
  	}
  }

}
